#include "stdafx.h"
//-------------------------------------------------------------------------------------------------
//ImageProcSample.h  -            
//-------------------------------------------------------------------------------------------------
//FreeBSD License
//Copyright 2012 Will Stott All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are
//permitted provided that the following conditions are met:
//
//   1. Redistributions of source code must retain the above copyright notice, this list of
//      conditions and the following disclaimer.
//
//   2. Redistributions in binary form must reproduce the above copyright notice, this list
//      of conditions and the following disclaimer in the documentation and/or other materials
//      provided with the distribution.
//
//THIS SOFTWARE IS PROVIDED BY Will Stott ``AS IS'' AND ANY EXPRESS OR IMPLIED
//WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
//FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Will Stott OR
//CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
//CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
//ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
//ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//The views and conclusions contained in the software and documentation are those of the
//authors and should not be interpreted as representing official policies, either expressed
//or implied, of Will Stott.
//
//----Authors--------------------------------------------------------------------------------------
//created by: will.stott@ucl.ac.uk Apr 2012
//
//----Comments-------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------
#include "ImageProcSample.h"

#include "ImageProcSample.h"
#include "DummyOne.h"
#include "DummyTwo.h"
#include "ProcParamsInputs.h"
#include "ProcParamsOutputs.h"

CImageProcSample::CImageProcSample() : _DoneCnt(0), _ImageFn('\0')
{
	AddImageFunction (new DummyOne() );

	_Proceed.RedLight();
	_Done.RedLight();
	_thread.Resume ();

	return;
}

CImageProcSample::~CImageProcSample()
{
	_ipParams = '\0';
	if (_ImageFn != '\0')
		delete _ImageFn;
	Kill ();
}

bool  CImageProcSample::AddImageFunction (IImageProcFn *ImageFn )
{
	bool rc = false;

	if ( (ImageFn != '\0') && (ImageFn->IsApiValid()))
	{
		_ImageFn = ImageFn;
		rc = true;
	}

	return rc;
}

void	CImageProcSample::Setup(const char *FunctionName, ProcParamsOutputs *output, ProcParamsInputs *input) //this means ImageProcDummy must reference ImageExptLib
{
	_ImageFn = '\0';

	if ((_ipParams = input) != '\0')
	{
		if (_ImageFn == '\0')
		{

		}
	}

	if ((_opParams = output) != '\0')
	{
		if (_ImageFn == '\0')
		{
			//set returncode
		}
	}

	//setup variables from input. param 1 = function name	

	//setup mutex - stop, done
	_Proceed.GreenLight();
}


void	CImageProcSample::Loop()
{
	int fnReturnCode = 0;	//0 == ok

	if (_ImageFn == '\0')
		fnReturnCode = 1;
	else
	{
		try
		{
			_Proceed.Wait();		//wait for thread creater to call Setup and for Setup call _Proceed.GreenLight()

			for ( ; ; )
			{
				//start looptime
				if (_isDying) 		//OnCancel() -> thread being deleted
				{
					fnReturnCode = 2;	//user cancel
					break;
				}
				else
				{
					if (_DoneCnt > 10)
						break;			//image processed		
					else
					{

						_ImageFn->Process(_DoneCnt);		//TODO: implement as a fn pointer according to input param FunctionName - do some useful work like processing a part of the image
						_DoneCnt++;		
					}

				}
			}
		}
		catch (...)
		{
			fnReturnCode = 3; // DummyFnexception
		}
	}
	SetupOutputParams(fnReturnCode);		//abend if caller presses cancel or thread times out

}


void	CImageProcSample::SetupOutputParams(int returncode)
{
	Lock lock();

		//set resultCode = fnreturnCode
		//set function specific results
		//	foreach(val in fnresultset)
		//		name.Format("Prefix-%c-%d", val->GetAxis(), val->GetIndex()");	//Prefix-X-1
		//		output->AddParam(new ProcParamString(name, val->getString()));
	_Done.GreenLight();

	return;
}



//-------------------------------------------------------------------------------------------------
//			E N D   O F   F I L E
//-------------------------------------------------------------------------------------------------
